<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=8">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/public/kube.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/public/main.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/themes/easyui.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/style/jsmind.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="__SKIN__/jsmind/easyui/themes/icon.css">

    <!--[if lt IE 7]>
    <script type="text/javascript" src="__SKIN__/jsmind/style/entypo/lte-ie7.js"></script>
    <![endif]-->
    <title>创建合同</title>
</head>

<body>

<div>
</div>

<div id="jsmind_container">
    <a id="add_root_btn" href="#" class="easyui-linkbutton">显示数据</a>
    <div></div>
</div>

<div id="mm" class="easyui-menu" style="width:120px; z-index:99999">
    <!--<div data-options="iconCls:'icon-add'">
        <span>增加标签</span>
        <div style="width:150px;">
            <div onClick="add_node()">增加标签</div>
        </div>
    </div>-->
    <div data-options="iconCls:'icon-add'" onClick="add_node()" >添加标签</div>
    <div class="menu-sep"></div>
    <div data-options="iconCls:'icon-edit'" onClick="modify_node()">编辑标签</div>
    <div data-options="iconCls:'icon-remove'"  onClick="remove_node()">删除标签</div>
    <div data-options="iconCls:'icon-coins'"onClick="set_price()">设置价格</div>
    <div data-options="iconCls:'icon-large-shapes'" onClick="edit_combo">
        <span>套餐分类</span>
        <div style="width:150px;" class="menu-text">
            <input type="checkbox" id="combo1" name="combo" checked value="">套餐一<br/>
            <input type="checkbox" id="combo2" name="combo" value="">套餐二<br/>
            <input type="checkbox" id="combo3" name="combo" value="">套餐三<br/>
            <a id="combo_btn" href="#" class="easyui-linkbutton">完成</a>
        </div>
    </div>
</div>

<div id="win"></div>

<script src="__SKIN__/jsmind/js/jquery-1.4.4.min.js"></script>
<script src="__SKIN__/jsmind/easyui/jquery.easyui.min.js"></script>
<script src="__SKIN__/jsmind/js/jsmind.js" type="text/javascript"></script>
<script src="__SKIN__/jsmind/js/jsmind.draggable.js" type="text/javascript"></script>
<script src="__SKIN__/jsmind/js/jquery.contextmenu.r2.js" type="text/javascript"></script>
<script src="__SKIN__/jsmind/js/html5shiv.js" type="text/javascript"></script>
<script type="text/javascript">
    var _jm = null;
    var default_price = 1000;
    var data = [
        /*{"id":"root", "isroot":true, "topic":"一、二、三次产业","isreadonly":"true"},

        {"id":"sub1", "parentid":"root", "topic":"第一产业（90）","isreadonly":"true"},
        {"id":"sub12", "parentid":"sub1", "topic":"农、林、牧、渔业（40）","isreadonly":"true"},
        {"id":"sub121", "parentid":"sub12", "topic":"农业"},
        {"id":"sub122", "parentid":"sub12", "topic":"林业"},
        {"id":"sub123", "parentid":"sub12", "topic":"畜牧业"},
        {"id":"sub124", "parentid":"sub12", "topic":"渔业"},
        {"id":"sub125", "parentid":"sub12", "topic":"农、林、牧、渔服务业",'combo1':true,'combo2':true,'combo3':true},

        {"id":"sub2", "parentid":"root", "topic":"第二产业（130）","isreadonly":"true"},
        {"id":"sub21", "parentid":"sub2", "topic":"采矿业（30）","isreadonly":"true"},
        {"id":"sub22", "parentid":"sub2", "topic":"制造业（100）","isreadonly":"true"},
        {"id":"sub23", "parentid":"sub2", "topic":"电力、燃气及水的生产和供应业（100）","isreadonly":"true"},
        {"id":"sub24", "parentid":"sub2", "topic":"建筑业（100）","isreadonly":"true"},

        {"id":"sub3", "parentid":"root", "topic":"第三产业（0）"},
        {"id":"sub31", "parentid":"sub3", "topic":"交通运输、仓储和邮政业（100）","isreadonly":"true"},
        {"id":"sub311", "parentid":"sub31", "topic":"铁路运输业（0）"},
        {"id":"sub312", "parentid":"sub31", "topic":"道路运输业（0）"},
        {"id":"sub313", "parentid":"sub31", "topic":"城市公共交通业（0）"},
        {"id":"sub314", "parentid":"sub31", "topic":"水上运输业（0）"},
        {"id":"sub315", "parentid":"sub31", "topic":"航空运输业（0）"},
        {"id":"sub3", "parentid":"sub31", "topic":"管道运输业（0）"},
        {"id":"sub32", "parentid":"sub3", "topic":"信息传输、计算机服务和软件业（87）","isreadonly":"true"},
        {"id":"sub33", "parentid":"sub3", "topic":"批发和零售业（35）","isreadonly":"true"},
        {"id":"sub34", "parentid":"sub3", "topic":"住宿和餐饮业（87）","isreadonly":"true"},
        {"id":"sub35", "parentid":"sub3", "topic":"金融业（162）","isreadonly":"true"},
        {"id":"sub36", "parentid":"sub3", "topic":"房地产业（11）","isreadonly":"true"},
        {"id":"sub37", "parentid":"sub3", "topic":"租赁和商务服务业（210）","isreadonly":"true"},
        {"id":"sub38", "parentid":"sub3", "topic":"科学研究、技术服务和地质勘查业（56）","isreadonly":"true"},
        {"id":"sub39", "parentid":"sub3", "topic":"水利、环境和公共设施管理业（81）","isreadonly":"true"},
        {"id":"sub310", "parentid":"sub3", "topic":"居民服务和其他服务业（121）","isreadonly":"true"},*/
    ];
    $.messager.defaults = { ok: "是", cancel: "否" };  //修改显示文字
    function load_jsmind(r){
        if(r != undefined && r != null && r != ''){
            var topic = r;
        }else {
            var topic = '公司合同';
        }
        if(data==null || data ==''){
            data = [{"id":"root", "isroot":true, "topic":topic,"isreadonly":"true",'combo1':true,'combo2':true,'combo3':true}];
        }
        var mind = {
            "meta":{
                "name":"demo",
                "author":"277099040@163.com",
                "version":"0.2",
            },
            "format":"node_array",
            "data":data,
        //"data":[
            //{"id":"root", "isroot":true, "topic":"一、二、三次产业","isreadonly":"true"},

            /* {"id":"sub1", "parentid":"root", "topic":"第一产业（90）","isreadonly":"true"},
             {"id":"sub12", "parentid":"sub1", "topic":"农、林、牧、渔业（40）","isreadonly":"true"},
             {"id":"sub121", "parentid":"sub12", "topic":"农业"},
             {"id":"sub122", "parentid":"sub12", "topic":"林业"},
             {"id":"sub123", "parentid":"sub12", "topic":"畜牧业"},
             {"id":"sub124", "parentid":"sub12", "topic":"渔业"},
             {"id":"sub125", "parentid":"sub12", "topic":"农、林、牧、渔服务业"},

             {"id":"sub2", "parentid":"root", "topic":"第二产业（130）","isreadonly":"true"},
             {"id":"sub21", "parentid":"sub2", "topic":"采矿业（30）","isreadonly":"true"},
             {"id":"sub22", "parentid":"sub2", "topic":"制造业（100）","isreadonly":"true"},
             {"id":"sub23", "parentid":"sub2", "topic":"电力、燃气及水的生产和供应业（100）","isreadonly":"true"},
             {"id":"sub24", "parentid":"sub2", "topic":"建筑业（100）","isreadonly":"true"},

             {"id":"sub3", "parentid":"root", "topic":"第三产业（0）"},
             {"id":"sub31", "parentid":"sub3", "topic":"交通运输、仓储和邮政业（100）","isreadonly":"true"},
             {"id":"sub311", "parentid":"sub31", "topic":"铁路运输业（0）"},
             {"id":"sub312", "parentid":"sub31", "topic":"道路运输业（0）"},
             {"id":"sub313", "parentid":"sub31", "topic":"城市公共交通业（0）"},
             {"id":"sub314", "parentid":"sub31", "topic":"水上运输业（0）"},
             {"id":"sub315", "parentid":"sub31", "topic":"航空运输业（0）"},
             {"id":"sub3", "parentid":"sub31", "topic":"管道运输业（0）"},
             {"id":"sub32", "parentid":"sub3", "topic":"信息传输、计算机服务和软件业（87）","isreadonly":"true"},
             {"id":"sub33", "parentid":"sub3", "topic":"批发和零售业（35）","isreadonly":"true"},
             {"id":"sub34", "parentid":"sub3", "topic":"住宿和餐饮业（87）","isreadonly":"true"},
             {"id":"sub35", "parentid":"sub3", "topic":"金融业（162）","isreadonly":"true"},
             {"id":"sub36", "parentid":"sub3", "topic":"房地产业（11）","isreadonly":"true"},
             {"id":"sub37", "parentid":"sub3", "topic":"租赁和商务服务业（210）","isreadonly":"true"},
             {"id":"sub38", "parentid":"sub3", "topic":"科学研究、技术服务和地质勘查业（56）","isreadonly":"true"},
             {"id":"sub39", "parentid":"sub3", "topic":"水利、环境和公共设施管理业（81）","isreadonly":"true"},
             {"id":"sub310", "parentid":"sub3", "topic":"居民服务和其他服务业（121）","isreadonly":"true"},*/
           //  ]
        };
        var options = {
            container:'jsmind_container',
            editable:true,
            theme:'primary',
            mode :'full'
        }
        _jm = jsMind.show(options,mind);
    }

    function get_selected_nodeid(){
        var selected_node = _jm.get_selected_node();
        if(!!selected_node){
            return selected_node.id;
        }else{
            return null;
        }
    }

    function set_price(){
        $.messager.prompt('对该标签设置价格', '请输入价格', function(r){
            if (r){
                var data = _jm.get_data('node_array').data; //所有数据
                var selected_id = get_selected_nodeid();//获取节点id
                update_parent_price(data,selected_id,r);
            }
        });
    }

    function update_parent_price(data,selected_id,price,is_parent = 0){
        price = parseFloat(price);
        if(data != null || data != undefined){
            for(let key  in data){
                if(data[key].id == selected_id){
                    var topic = _jm.get_node(selected_id).topic; //获取当前节点所显示的文本
                    var slip_num = topic.indexOf("/");         //获取 /  在字符串中的位置
                    price =  price.toFixed(2);
                    console.log(typeof price);
                    if(slip_num == -1){
                        r = topic + '/'+ price;  //如果没有设置价格就直接赋值
                    }else {
                        topic = topic.substring(0,slip_num)     //截取字符串，再赋值
                        r = topic + '/'+ price;
                    }
                    _jm.get_node(selected_id).price = price; //给节点的价格属性赋值
                    _jm.update_node(selected_id, r);//更改内容
                    var parent_node = _jm.get_node(selected_id).parent; //获取父节点
                    //如果没有父节点就返回
                    if(parent_node === undefined){
                        return false;
                    }

                    console.log(parent_node);
                    var children = parent_node.children;
                    var children_price = 0;
                    //如果没有子节点就返回
                    if(children === undefined){
                        return false;
                    }
                    for(let children_key  in children){
                        if(children[children_key].price != undefined){
                            children_price = parseFloat(children_price) + parseFloat(children[children_key].price);
                        }
                    }
                    if(parent_node != null){
                        console.log(parent_node.id);
                        update_parent_price(data,parent_node.id,children_price,is_parant = 1)
                    }
                }
            }
        }
    }

    function modify_node(){
        $.messager.prompt('编辑标签', '请输入新的标签名', function(r){

            var slip_num = r.indexOf("/");         //获取 /  在字符串中的位置
            if(slip_num == -1){
                alert('格式不对，/ 前面是节点名称，/ 前面是节点价格');
                return false;
            }
            var price = r.substring(slip_num+1);
            console.log(typeof price);
            console.log(price);
            price = parseFloat(price);

            var data = _jm.get_data('node_array').data; //所有数据
            var selected_id = get_selected_nodeid();//获取节点id

            if (r){
                var selected_id = get_selected_nodeid();
                //_jm.enable_edit();
                _jm.update_node(selected_id, r);
                update_parent_price(data,selected_id,price);
                //_jm.disable_edit();
            }
        });
    }

    function remove_node(){
       /* var selected_id = get_selected_nodeid();
        if(!selected_id){alert('请选择一个节点');return;}
        _jm.remove_node(selected_id);*/
        $.messager.confirm("删除标签", "确定要删除该标签吗？", function (data) {
            if (data) {
                var selected_id = get_selected_nodeid();
                if(!selected_id){alert('请选择一个节点');return;}
                _jm.remove_node(selected_id);
            }
        });
       /* $.messager.confirm('删除标签', '确定要删除该标签吗', function(data){
            if (data) {
                var selected_id = get_selected_nodeid();
                console.log(selected_id);
                //_jm.remove_node();
                _jm.remove_node(selected_id);
                //_jm.remove_node();
            }else {

            }
        });*/
    }

    function add_node(){
        $.messager.prompt('新增标签', '请输入标签名', function(r){
            if (r){
                var nodeid = jsMind.util.uuid.newid();
                var topic = r + '/'+default_price;
                //_jm.enable_edit();
                //扩展方法， right 将子节点增加到右侧， left 将子节点增加到左侧，默认右侧
                var node = _jm.add_node(_jm.get_selected_node(), nodeid, topic, null, "right");
                console.log(node);

                var data = _jm.get_data('node_array').data; //所有数据
                var selected_id = node.id;//获取节点id
                update_parent_price(data,selected_id,default_price);
               // _jm.disable_edit();
                //ajaxSaveDate(_jm.mind.root);
            }
        });
    }

    function right()
    {
        console.log(e.button);
        var e=window.event;
        if(e.button=="2"){
            console.debug("右键");
        }
    }

    function show_selected(){
        console.log(e.button);
        var selected_node = _jm.get_selected_node();
        if(!!selected_node){
            return selected_node.data.isreadonly;
        }else{
            console.debug("nothing");
        }
    }

    function toggle_node(){
        var selected_id = get_selected_nodeid();
        if(!selected_id){alert('请先选择一个节点');}
        _jm.toggle_node(selected_id);
    }

    function edit_combo(obj){
        var selected_node = _jm.get_selected_node();
        console.log(selected_node);
    }

    function show_detail(){
        var href='CompanyLists.html';
        $('#win').window({
            title:"企业明细",
            width:1200,height:600,
            modal:true,shadow: true,collapsible:false,minimizable:false,maximizable:false,
            content:'<iframe scrolling="no" frameborder="0"  src="'+href+'" style="width:100%;height:99.5%;"></iframe>'
        });
    }




    $('#add_root_btn').click(function(){
       ajaxSaveDate();
    });

    //给该节点提交套餐信息
    $('#combo_btn').click(function () {
        var combo1 = $('#combo1').attr('checked');
        var combo2 = $('#combo2').attr('checked');
        var combo3 =  $('#combo3').attr('checked');

       /* console.log(combo1);
        console.log(combo2);
        console.log(combo3);
        console.log(_jm);
        console.log(_jm.get_selected_node().id);*/
        _jm.get_selected_node().data={'combo1':combo1,'combo2':combo2,'combo3':combo3};
        console.log(_jm.get_selected_node());
        /*console.log(_jm.get_meta());
        console.log(_jm.mind.nodes);*/
        //console.log(_jm.get_data('node_array'));
        $('#mm').menu('hide');
    });

    function ajaxSaveDate() {
           var mind_data = _jm.get_data('node_array');
           console.log(mind_data.data);
           var mind_string = jsMind.util.json.json2string(mind_data);
           console.log(mind_string);

        //getdata(obj);
        /*for (let key in obj) {
            if(key == 'id' && obj['id']== 'root'){
                console.log(obj['id']); // foo, bar
                console.log(obj['index']);
                console.log(obj['topic']);
            }
        }*/
    }

    function getdata(obj){
        var state = 1;
        for (let key in obj) {
            if(state == 1){
                if(obj['children'] != null || obj['children'] != undefined){
                    getdata(obj['children']);
                }
                console.log(obj.id); // foo, bar
                console.log(obj['index']);
                console.log(obj['topic']);
                state = 0;
                return false;
            }
        }
    }

    $(function(){
        console.log(data);
        if(data==null || data =='' || data==undefined){
            $.messager.prompt('编辑根节点', '请填写合同名称', function(r){
                if (r){
                    console.log(r);
                    load_jsmind(r);
                    $("jmnode").live('contextmenu',function(e){
                        /*console.log(22);
                        data = [{"id":"root", "isroot":true, "topic":r,"isreadonly":"true",'combo1':true,'combo2':true,'combo3':true}];
                        console.log(data);
                        console.log(33);*/
                        load_click();
                        e.preventDefault();
                        $('#mm').menu('show', {
                            left: e.pageX,
                            top: e.pageY,
                            hideOnUnhover: false
                        });
                    });
                }
            });
        }else{
            load_jsmind();
            $("jmnode").live('contextmenu',function(e){
                $('#combo1').val(false);
                $('#combo1').attr('checked',false);
                $('#combo2').val(false);
                $('#combo2').attr('checked',false);
                $('#combo3').val(false);
                $('#combo3').attr('checked',false);

                var selected_id = get_selected_nodeid();
                console.log(selected_id);
                //var data = _jm.get_selected_node().data
                var data = _jm.get_data('node_array').data;
                console.log(data);
                if(data != null || data != undefined){
                    for(let key  in data){

                        if(data[key].id == selected_id){
                            console.log(data[key].combo1);
                            var combo1 = data[key].combo1 ? data[key].combo1 : false;
                            var combo2 = data[key].combo2 ? data[key].combo2 : false;
                            var combo3 = data[key].combo3 ? data[key].combo3 : false;

                            $('#combo1').val(combo1);
                            $('#combo1').attr('checked',combo1);
                            $('#combo2').val(combo2);
                            $('#combo2').attr('checked',combo2);
                            $('#combo3').val(combo3);
                            $('#combo3').attr('checked',combo3);
                        }

                        /*if(combo_data[key] != false && combo_data[key] != null && combo_data[key] != undefined){
                            console.log(combo_data[key]);
                            $('#'+ key).val(combo_data[key]);
                            $('#'+ key).attr('checked',true);
                        }*/
                    }
                }
                /*var combo_data = null;
                for(j = 0,len=data.length; j < len; j++) {
                    if(data[j].id == selected_id){
                        console.log(data[j]);
                        combo_data = data[j].data;
                    }
                }*/

                /*if(combo_data != null || combo_data != undefined){
                    for(let key  in combo_data){
                        if(combo_data[key] != false && combo_data[key] != null && combo_data[key] != undefined){

                            console.log(combo_data[key]);
                            $('#'+ key).val(combo_data[key]);
                            $('#'+ key).attr('checked',true);
                        }
                    }
                }*/
                e.preventDefault();
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY,
                    hideOnUnhover: false
                });
            });
        }
    });

    function load_click(){
        $('#combo1').val(false);
        $('#combo1').attr('checked',false);
        $('#combo2').val(false);
        $('#combo2').attr('checked',false);
        $('#combo3').val(false);
        $('#combo3').attr('checked',false);

        var selected_id = get_selected_nodeid();
        console.log(selected_id);
        //var data = _jm.get_selected_node().data
        var data = _jm.get_data('node_array').data;
        console.log(data);
        if(data != null || data != undefined){
            for(let key  in data){

                if(data[key].id == selected_id){
                    console.log(data[key].combo1);
                    var combo1 = data[key].combo1 ? data[key].combo1 : false;
                    var combo2 = data[key].combo2 ? data[key].combo2 : false;
                    var combo3 = data[key].combo3 ? data[key].combo3 : false;

                    $('#combo1').val(combo1);
                    $('#combo1').attr('checked',combo1);
                    $('#combo2').val(combo2);
                    $('#combo2').attr('checked',combo2);
                    $('#combo3').val(combo3);
                    $('#combo3').attr('checked',combo3);
                }

                /*if(combo_data[key] != false && combo_data[key] != null && combo_data[key] != undefined){
                    console.log(combo_data[key]);
                    $('#'+ key).val(combo_data[key]);
                    $('#'+ key).attr('checked',true);
                }*/
            }
        }
    }
</script>
</body>
</html>
